package com.molin.setTest;

import java.util.TreeSet;

public class MyTreeSet {
    public static void main(String[] args) {
//        treeSet_Interger();
        TreeSet<Student> studentTreeSet=new TreeSet<>();
        studentTreeSet.add(new Student(23,"xiaoli"));
        studentTreeSet.add(new Student(25,"xiaowang"));
        studentTreeSet.add(new Student(24,"xiaoming"));
        System.out.println(studentTreeSet);
    }

    static class Student implements Comparable<Student>{
        private int age;
        private String name;

        public Student(int age, String name) {
            this.age = age;
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "age=" + age +
                    ", name='" + name + '\'' +
                    '}';
        }

        @Override
        public int compareTo(Student o) {
            //按照年龄进行排序，年龄不能重复
            //o.age表示上一个对象的年龄
            return  -(this.age-o.age);
        }
    }

    public static void treeSet_Interger() {
        //TreeSet存储Integer类型
        TreeSet<Integer> ts=new TreeSet<>();
        ts.add(5);
        ts.add(4);
        ts.add(3);
        ts.add(0);
        ts.add(10);
        System.out.println(ts);
        //[0, 3, 4, 5, 10]
    }
}